<?php

namespace Sharks\Core\Make;

use Sharks\Core\AppContext;
use Sharks\Core\Cfg\AppConfig;
use Sharks\Core\Http\Routing\MiddlewareCollection;
use Sharks\Core\Http\Routing\MiddlewaresConfReader;
use Sharks\Core\Http\Routing\RouteCollection;
use Sharks\Core\Http\Routing\RouteConfReader;
use Sharks\Core\Http\Routing\RouteControl;
use Sharks\Core\Module;
use Sharks\Core\Support\Storage\ArrayLoader;
use Sharks\Core\Support\Storage\StorageArrayUtils;

/**
 * Route
 * 依赖：
 * 		ModuleMaker
 */
class RouteMaker extends Maker {

	/**
	 * 开发模式。即时读取各模块文件。
	 * @param  AppContext $app [description]
	 * @return [type]          [description]
	 */
	public function dev(AppContext $app) {
        $routeCollection = RouteConfReader::read($app->path()->app->source . "/Http/routes.conf");
        $middleCollection = MiddlewaresConfReader::read($app->path()->app->source . "/Http/middlewares.conf");

		foreach (Module::all() as $name => $module) {
            $routeCollection->merge(RouteConfReader::read($module->path()->http . '/routes.conf'));
            $middleCollection->merge(MiddlewaresConfReader::read($module->path()->http . '/middlewares.conf'));
		}
		RouteControl::addRouteCollection($routeCollection);
		RouteControl::addMiddlewareCollection($middleCollection);
	}
	
	/**
	 * 服务模式。读取预定义文件。
	 * TODO 还未完成
	 */
	public function server(AppContext $app) {
		$routes = ArrayLoader::load($app->path()->storage . '/routes.php', array());
		$middlewares = ArrayLoader::load($app->path()->storage . '/middlewares.php', array());
		$reverses = ArrayLoader::load($app->path()->storage . '/reverses.php', array());
	}

	/**
	 * 前置依赖
	 * @return array
	 */
	public function makers() {
		return array(ModuleMaker::class);
	}
}